<?php
session_start();

include("config.php");

$form = 1;
$session = 1;

if(isset($_SESSION['pseudo']))
{

	$link = mysql_connect ('localhost', DB_USER_NAME, DB_PASSWORD) or die ("Cannot join database");
	mysql_select_db (DB_NAME);

	//On verifie si l'on a un premier acces à la page
	if(isset($_POST['firstname']))
	{
		//On verifie que tous les champs soient remplis
		if(empty($_POST['firstname']) || empty($_POST['lastname']) || empty($_POST['email']) )
		{
			echo "Empty fields are not allowed <br>";
		}
		else
		{	
				//On verifie si le pseudo n'existe pas déjà
			$requete = "UPDATE user SET prenom='" . $_POST['firstname'] . "', nom='" . $_POST['lastname'] . "', 
							email='" . $_POST['email'] . "' WHERE pseudo='" . $_SESSION['pseudo']. "'";
			
			$result = mysql_query ($requete);	

			echo "Your informations have been succesfully updated <br>";
			$form = 0;
				
		}

	}

	if($form)
	{
		$requete = "SELECT * FROM user WHERE pseudo='" . $_SESSION['pseudo'] . "'";
		$result = mysql_query ($requete);	

		while ($row = mysql_fetch_array($result)) {
  			$firstname = $row['prenom'];
			$lastname = $row['nom'];
			$email = $row['email'];
		}

	}
	mysql_close($link);		

}//fin du if de session
else
{
	$session = 0;
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" >
	<head>
		<title>Life Stream - Member</title>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
	</head>
   
	<body>		
		<?php
		if($session)
		{
			if($form)
			{
				echo "Welcome " . $_SESSION['pseudo'] . "<br>";
				?>
				Your datas:</br>
				<form method="post" action="member.php">
				<p>Pseudo: <input type="text" name="pseudo" value="<? echo $_SESSION['pseudo']?>" disabled="disabled"/></p>
				<p>First name: <input type="text" name="firstname" value="<? echo $firstname?>"/></p>
				<p>Last name: <input type="text" name="lastname" value="<? echo $lastname?>"/></p>
				<p>Email: <input type="text" name="email" value="<? echo $email?>"/></p>
				<p><input type="submit" value="Valider" /></p>
				</form>
				<?
			}
		}
		else
		{
			echo "You must be connected<br>";
		}
		?>
	
	</body>
</html>
